package db

import (
	"errors"
	"hr/api/dto"

	"github.com/spf13/cast"
)

type Salary struct {
	AppModel
	UserId    string `gorm:"size:64" json:"user_id"`
	Name      string `gorm:"size:64" json:"name"`
	DateMonth string `gorm:"size:64" json:"date_month"`
	WorkDays  string `gorm:"size:64" json:"work_days"` //工作天数
	RealDays  string `gorm:"size:64" json:"real_days"` //实际天数
	BasePay   string `gorm:"size:64" json:"base_pay"`  //基本工资
	PostPay   string `gorm:"size:64" json:"post_pay"`  //岗位工资
	WorkPay   string `gorm:"size:64" json:"work_pay"`  //出勤工资
	WorkAge   string `gorm:"size:64" json:"work_age"`  //工龄
	DuePay    string `gorm:"size:64" json:"due_pay"`   //应得工资
	Excellent string `gorm:"size:64" json:"excellent"` //优秀
	OtherPay  string `gorm:"size:64" json:"other_pay"` //其他
	Insurance string `gorm:"size:64" json:"insurance"` //保险
	Merits    string `gorm:"size:64" json:"merits"`    //绩效工资
	UnionFee  string `gorm:"size:64" json:"union_fee"` //工会经费
	RealPay   string `gorm:"size:64" json:"real_pay"`  //实得工资
	Remark    string `json:"remark"`                   //备注
	CreateBy  string `gorm:"size:128" json:"createBy"` // 创建人
}

func (this *Salary) TableName() string {
	return "salary"
}

type SalaryPageModel struct {
	Total    int64    `json:"total"`
	Result   []Salary `json:"result"`
	PageNo   int      `json:"pageNo"`
	PageSize int      `json:"pageSize"`
}

func GetSalaryPaging(pagination dto.Pagination, name string) (SalaryPageModel, error) {
	result := SalaryPageModel{
		Total:    0,
		Result:   make([]Salary, 0),
		PageNo:   pagination.PageNo,
		PageSize: pagination.PageSize,
	}
	offset := (pagination.PageNo - 1) * pagination.PageSize
	dbs := GetInstance().Model(&Salary{})
	if name != "" {
		dbs = dbs.Where("name LIKE ?", "%"+name+"%")
	}

	dbs = dbs.Order("created_at desc")

	if err := dbs.Offset(offset).Limit(pagination.PageSize).Find(&result.Result).Error; err != nil {
		return result, err
	}
	if err := dbs.Offset(-1).Limit(-1).Count(&result.Total).Error; err != nil {
		return result, err
	}
	return result, nil
}

func CreateSalary(p Salary, user string) (Salary, error) {
	p.CreateBy = user
	var u User
	GetInstance().Where("name = ?", p.Name).First(&u)
	if u.ID > 0 {
		p.UserId = cast.ToString(u.ID)
	}
	if err := GetInstance().Create(&p).Error; err != nil {
		return p, errors.New("创建失败")
	}
	return p, nil
}

func DeleteSalary(id uint) error {
	f := Salary{}
	err := GetInstance().Unscoped().Where("id = ?", id).Delete(&f).Error
	if err != nil {
		return err
	}
	return nil
}

func GetSalaryPagingByUser(pagination dto.Pagination, userId string) (SalaryPageModel, error) {
	result := SalaryPageModel{
		Total:    0,
		Result:   make([]Salary, 0),
		PageNo:   pagination.PageNo,
		PageSize: pagination.PageSize,
	}
	offset := (pagination.PageNo - 1) * pagination.PageSize
	dbs := GetInstance().Model(&Salary{}).Where("user_id=?", userId).Order("created_at desc")

	if err := dbs.Offset(offset).Limit(pagination.PageSize).Find(&result.Result).Error; err != nil {
		return result, err
	}
	if err := dbs.Offset(-1).Limit(-1).Count(&result.Total).Error; err != nil {
		return result, err
	}
	return result, nil
}
